home *** CD-ROM | disk | FTP | other *** search
- package javax.swing.text.html;
-
- import java.awt.Color;
- import java.io.IOException;
- import java.io.Writer;
- import java.util.Enumeration;
- import javax.swing.text.AbstractDocument;
- import javax.swing.text.AbstractWriter;
- import javax.swing.text.AttributeSet;
- import javax.swing.text.BadLocationException;
- import javax.swing.text.DefaultStyledDocument;
- import javax.swing.text.Element;
- import javax.swing.text.ElementIterator;
- import javax.swing.text.Style;
- import javax.swing.text.StyleConstants;
- import javax.swing.text.StyleContext;
- import javax.swing.text.StyledDocument;
-
- public class MinimalHTMLWriter extends AbstractWriter {
- private static final int BOLD = 1;
- private static final int ITALIC = 2;
- private static final int UNDERLINE = 4;
- private static final CSS css = new CSS();
- private int fontMask = 0;
- int startOffset = 0;
- int endOffset = 0;
- private AttributeSet fontAttributes;
-
- public MinimalHTMLWriter(Writer var1, StyledDocument var2) {
- super(var1, var2);
- }
-
- public MinimalHTMLWriter(Writer var1, StyledDocument var2, int var3, int var4) {
- super(var1, var2, var3, var4);
- }
-
- protected void endFontTag() throws IOException {
- ((AbstractWriter)this).write('\n');
- this.writeEndTag("</font>");
- this.fontAttributes = null;
- }
-
- protected boolean inFontTag() {
- return this.fontAttributes != null;
- }
-
- protected boolean isText(Element var1) {
- return var1.getName() == "content";
- }
-
- private void setFontMask(AttributeSet var1) {
- if (StyleConstants.isBold(var1)) {
- this.fontMask |= 1;
- }
-
- if (StyleConstants.isItalic(var1)) {
- this.fontMask |= 2;
- }
-
- if (StyleConstants.isUnderline(var1)) {
- this.fontMask |= 4;
- }
-
- }
-
- protected void startFontTag(String var1) throws IOException {
- boolean var2 = false;
- if (this.inFontTag()) {
- this.endFontTag();
- var2 = true;
- }
-
- this.writeStartTag("<font style=\"" + var1 + "\">");
- if (var2) {
- ((AbstractWriter)this).indent();
- }
-
- }
-
- protected void text(Element var1) throws IOException, BadLocationException {
- String var2 = ((AbstractWriter)this).getText(var1);
- if (var2.length() > 0 && var2.charAt(var2.length() - 1) == '\n') {
- var2 = var2.substring(0, var2.length() - 1);
- }
-
- if (var2.length() > 0) {
- ((AbstractWriter)this).write(var2);
- }
-
- }
-
- public void write() throws IOException, BadLocationException {
- this.writeStartTag("<html>");
- this.writeHeader();
- this.writeBody();
- this.writeEndTag("</html>");
- }
-
- protected void writeAttributes(AttributeSet var1) throws IOException {
- Enumeration var2 = var1.getAttributeNames();
-
- while(var2.hasMoreElements()) {
- Object var3 = var2.nextElement();
- if (var3 instanceof StyleConstants.ParagraphConstants || var3 instanceof StyleConstants.CharacterConstants || var3 instanceof StyleConstants.FontConstants || var3 instanceof StyleConstants.ColorConstants) {
- ((AbstractWriter)this).indent();
- ((AbstractWriter)this).write(var3.toString());
- ((AbstractWriter)this).write(':');
- ((AbstractWriter)this).write(css.styleConstantsValueToCSSValue((StyleConstants)var3, var1.getAttribute(var3)).toString());
- ((AbstractWriter)this).write(';');
- ((AbstractWriter)this).write('\n');
- }
- }
-
- }
-
- protected void writeBody() throws IOException, BadLocationException {
- ElementIterator var1 = ((AbstractWriter)this).getElementIterator();
- var1.current();
- Object var2 = null;
- this.writeStartTag("<body>");
- boolean var3 = false;
-
- while((var4 = var1.next()) != null) {
- if (((AbstractWriter)this).inRange(var4)) {
- if (var4 instanceof AbstractDocument.BranchElement) {
- if (var3) {
- this.writeEndParagraph();
- var3 = false;
- this.fontMask = 0;
- }
-
- this.writeStartParagraph(var4);
- } else if (this.isText(var4)) {
- this.writeContent(var4, var3 ^ true);
- var3 = true;
- } else {
- this.writeLeaf(var4);
- var3 = true;
- }
- }
- }
-
- if (var3) {
- this.writeEndParagraph();
- }
-
- this.writeEndTag("</body>");
- }
-
- protected void writeComponent(Element var1) throws IOException {
- }
-
- protected void writeContent(Element var1, boolean var2) throws IOException, BadLocationException {
- AttributeSet var3 = var1.getAttributes();
- this.writeNonHTMLAttributes(var3);
- if (var2) {
- ((AbstractWriter)this).indent();
- }
-
- this.writeHTMLTags(var3);
- this.text(var1);
- }
-
- private void writeEndMask(int var1) throws IOException {
- if (var1 != 0) {
- if ((var1 & 1) != 0) {
- ((AbstractWriter)this).write("</b>");
- }
-
- if ((var1 & 2) != 0) {
- ((AbstractWriter)this).write("</i>");
- }
-
- if ((var1 & 4) != 0) {
- ((AbstractWriter)this).write("</u>");
- }
- }
-
- }
-
- protected void writeEndParagraph() throws IOException {
- this.writeEndMask(this.fontMask);
- if (this.inFontTag()) {
- this.endFontTag();
- } else {
- ((AbstractWriter)this).write('\n');
- }
-
- this.writeEndTag("</p>");
- }
-
- protected void writeEndTag(String var1) throws IOException {
- ((AbstractWriter)this).decrIndent();
- ((AbstractWriter)this).indent();
- ((AbstractWriter)this).write(var1);
- ((AbstractWriter)this).write('\n');
- }
-
- protected void writeHTMLTags(AttributeSet var1) throws IOException {
- int var2 = this.fontMask;
- this.setFontMask(var1);
- int var3 = 0;
- int var4 = 0;
- if ((var2 & 1) != 0) {
- if ((this.fontMask & 1) == 0) {
- var3 |= 1;
- }
- } else if ((this.fontMask & 1) != 0) {
- var4 |= 1;
- }
-
- if ((var2 & 2) != 0) {
- if ((this.fontMask & 2) == 0) {
- var3 |= 2;
- }
- } else if ((this.fontMask & 2) != 0) {
- var4 |= 2;
- }
-
- if ((var2 & 4) != 0) {
- if ((this.fontMask & 4) == 0) {
- var3 |= 4;
- }
- } else if ((this.fontMask & 4) != 0) {
- var4 |= 4;
- }
-
- this.writeEndMask(var3);
- this.writeStartMask(var4);
- }
-
- protected void writeHeader() throws IOException {
- this.writeStartTag("<head>");
- this.writeStartTag("<style>");
- this.writeStartTag("<!--");
- this.writeStyles();
- this.writeEndTag("-->");
- this.writeEndTag("</style>");
- this.writeEndTag("</head>");
- }
-
- protected void writeImage(Element var1) throws IOException {
- }
-
- protected void writeLeaf(Element var1) throws IOException {
- ((AbstractWriter)this).indent();
- if (var1.getName() == "icon") {
- this.writeImage(var1);
- } else if (var1.getName() == "component") {
- this.writeComponent(var1);
- }
-
- }
-
- protected void writeNonHTMLAttributes(AttributeSet var1) throws IOException {
- String var2 = "";
- String var3 = "; ";
- if (!this.inFontTag() || !this.fontAttributes.isEqual(var1)) {
- Color var4 = (Color)var1.getAttribute(StyleConstants.Foreground);
- if (var4 != null) {
- var2 = var2 + "color: " + css.styleConstantsValueToCSSValue((StyleConstants)StyleConstants.Foreground, var4) + var3;
- }
-
- Integer var5 = (Integer)var1.getAttribute(StyleConstants.FontSize);
- if (var5 != null) {
- var2 = var2 + "font-size: " + var5 + var3;
- }
-
- String var6 = (String)var1.getAttribute(StyleConstants.FontFamily);
- if (var6 != null) {
- var2 = var2 + "font-family: " + var6 + var3;
- }
-
- if (var2.length() > 0) {
- this.startFontTag(var2);
- this.fontAttributes = var1;
- }
-
- }
- }
-
- private void writeStartMask(int var1) throws IOException {
- if (var1 != 0) {
- if ((var1 & 4) != 0) {
- ((AbstractWriter)this).write("<u>");
- }
-
- if ((var1 & 2) != 0) {
- ((AbstractWriter)this).write("<i>");
- }
-
- if ((var1 & 1) != 0) {
- ((AbstractWriter)this).write("<b>");
- }
- }
-
- }
-
- protected void writeStartParagraph(Element var1) throws IOException {
- AttributeSet var2 = var1.getAttributes();
- Object var3 = var2.getAttribute(StyleConstants.ResolveAttribute);
- if (var3 instanceof StyleContext.NamedStyle) {
- this.writeStartTag("<p class=" + ((StyleContext.NamedStyle)var3).getName() + ">");
- } else {
- this.writeStartTag("<p>");
- }
-
- }
-
- protected void writeStartTag(String var1) throws IOException {
- ((AbstractWriter)this).indent();
- ((AbstractWriter)this).write(var1);
- ((AbstractWriter)this).write('\n');
- ((AbstractWriter)this).incrIndent();
- }
-
- protected void writeStyles() throws IOException {
- DefaultStyledDocument var1 = (DefaultStyledDocument)((AbstractWriter)this).getDocument();
- Enumeration var2 = var1.getStyleNames();
-
- while(var2.hasMoreElements()) {
- Style var3 = var1.getStyle((String)var2.nextElement());
- if (var3.getAttributeCount() != 1 || !var3.isDefined(StyleConstants.NameAttribute)) {
- ((AbstractWriter)this).indent();
- ((AbstractWriter)this).write("p." + var3.getName());
- ((AbstractWriter)this).write(" {\n");
- ((AbstractWriter)this).incrIndent();
- this.writeAttributes(var3);
- ((AbstractWriter)this).decrIndent();
- ((AbstractWriter)this).indent();
- ((AbstractWriter)this).write("}\n");
- }
- }
-
- }
- }
-